﻿using Awsc.DataAcquisitionSys.Core.Service.Alarm.Interfaces;


namespace Awsc.DataAcquisitionSys.Core.Service.Alarm.Implement
{
    public interface IAlarmEngine
    {
        Task StartAsync(List<IAlarmRule> rules);
        Task StopAsync();
        Task AddRule(IAlarmRule rule);
        Task RemoveRule(string name);
        Task ActivateRule(string ruleName);
        Task DeactiveRule(string ruleName);
        Task AddAlarmMethod(string ruleName, string methodName);
        Task RemoveAlarmMethod(string ruleName, string methodName);
    }
    public class AlarmEngine: IAlarmEngine
    {
        private  List<IAlarmRule> rules = new List<IAlarmRule>();


        public async Task StartAsync(List<IAlarmRule> rules)
        {
            this.rules = rules;
            foreach (var rule in rules)
            {
                await rule.Activate();
            }
        }

        public async Task StopAsync()
        {
            foreach (var rule in rules)
            {
                await rule.Deactivate();
            }
        }

        public async Task AddRule(IAlarmRule rule)
        {
            if(rule==null)
            {
                return;
            }
            if (!rules.Any(r => r.Name == rule.Name))
            {
                rules.Add(rule);
               await rule.Activate();
            }
        }
        public async Task RemoveRule(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                return;
            }
            var rule = rules.FirstOrDefault(r => r.Name == name);
            if(rule!=null)
            {
                rules.Remove(rule);
               await rule.Deactivate();

            }
            this.rules.RemoveAll(r => r.Name == name);

        }
        public async Task ActivateRule(string ruleName)
        {
            if (string.IsNullOrWhiteSpace(ruleName))
            {
                return;
            }
            var rule = rules.FirstOrDefault(r => r.Name == ruleName);
            if (rule != null)
            {
                await rule.Activate();
            }
        }
        public async Task DeactiveRule(string ruleName)
        {
            if (string.IsNullOrWhiteSpace(ruleName) )
            {
                return;
            }
            var rule = rules.FirstOrDefault(r => r.Name == ruleName);
            if (rule != null)
            {
                await rule.Deactivate();
            }
        }
        public async Task AddAlarmMethod(string ruleName, string methodName)
        {
            if (string.IsNullOrWhiteSpace(ruleName) || string.IsNullOrWhiteSpace(methodName))
            {
                return;
            }
            var rule = rules.FirstOrDefault(r => r.Name == ruleName);
            if (rule != null)
            {

                var method = AlarmMethods.GetByName(methodName);
                rule.AddAlarmMethod(method);
            }
        }

        public async Task RemoveAlarmMethod(string ruleName,string methodName)
        {
            if (string.IsNullOrWhiteSpace(ruleName) || string.IsNullOrWhiteSpace(methodName))
            {
                return;
            }
            var rule = rules.FirstOrDefault(r => r.Name == ruleName);
            if (rule != null)
            {

                var method = AlarmMethods.GetByName(methodName);
                rule.RemoveAlarmMethod(method);
            }
        }


    }
}
